import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';

enum TYPE {
  '满减' = 1,
  '满折',
  '多件折扣',
  '多件立减',
}

enum USERTYPE {
  '不限制',
  'C端用户',
  'B端用户',
}

enum STATUS {
  '未开始' = 1,
  '进行中',
  '已结束',
  '已停止',
  '草稿',
  '待审核',
  '审核不通过',
  '删除',
}

@Injectable({
  providedIn: 'root'
})
export class RechargeService {
  serviceUrl = {
    dictUrl: '/dict/detail', // 字典查询
    uploadFileUrl: '/common/upload/file', // 上传

    addPromotion: '/userRechargePromotionInfo/save', // 保存促销
    updatePromotion: '/userRechargePromotionInfo/update', // 修改促销
    deletePromotion: '/userRechargePromotionInfo/delete/', // 删除促销
    promotionList: '/userRechargePromotionInfo/list', // 促销列表
    promotionDetail: '/userRechargePromotionInfo/details', // 促销详情
    promotionTrack: '/userRechargePromotionInfoLog/list', // 直降促销记录

    memberOptions: '/rechargeConfig/list', // 会员列表
    configList: '/rechargeConfig/configList', // 充值配置列表
    editConfig: '/rechargeConfig/update', // 编辑充值配置
    moveConfig: '/rechargeConfig/move', // 充值配置上移下移
  };
  TYPE = TYPE;
  USERTYPE = USERTYPE;
  STATUS = STATUS;

  constructor(public httpClient: HttpClient) {
  }

  // 文件上传
  fileUpload(file: any) {
    return this.httpClient.post(this.serviceUrl.uploadFileUrl, file).toPromise();
  }

  // 查询促销列表
  getPromotionList(body: any) {
    return this.httpClient.post(this.serviceUrl.promotionList, body).toPromise();
  }

  // 查询促销详情
  getPromotionDetail(id: any) {
    return this.httpClient.post(this.serviceUrl.promotionDetail, {id}).toPromise();
  }

  // 编辑促销
  editPromotion(body: any) {
    const url = body.id ? this.serviceUrl.updatePromotion : this.serviceUrl.addPromotion;
    return this.httpClient.post(url, body).toPromise();
  }

  // 删除促销
  deletePromotion(id: any) {
    return this.httpClient.get(this.serviceUrl.deletePromotion + id).toPromise();
  }

  // 直降促销记录
  getPromotionTrackList(body: any) {
    return this.httpClient.post(this.serviceUrl.promotionTrack, body).toPromise();
  }

  // 获取会员配置列表
  getMemberOptions() {
    return this.httpClient.post(this.serviceUrl.memberOptions, {}).toPromise();
  }

  // 获取充值配置列表
  getConfigList() {
    return this.httpClient.post(this.serviceUrl.configList, {}).toPromise();
  }

  // 编辑充值配置
  editConfig(body: any) {
    return this.httpClient.post(this.serviceUrl.editConfig, body).toPromise();
  }

  // 充值配置上移下移
  moveConfig(body: any) {
    return this.httpClient.post(this.serviceUrl.moveConfig, body).toPromise();
  }


  formatTime(date: any, type: 'date' | 'dateTime' | 'time') {
    if (!date || (typeof date) === 'string') {
      return date || '';
    }
    let result = '';
    const h = this.addZero(date.getHours());
    const m = this.addZero(date.getMinutes());
    const s = this.addZero(date.getSeconds());
    switch (type) {
      case 'date':
        // @ts-ignore
        result = `${date.getFullYear()}-${this.addZero(date.getMonth() + 1)}-${date.getDate()}`;
        break;
      case 'dateTime':
        // @ts-ignore
        result = `${date.getFullYear()}-${this.addZero(date.getMonth() + 1)}-${date.getDate()}` + ' ' + h + ':' + m + ':' + s;
        break;
      case 'time':
        // @ts-ignore
        result = h + ':' + m + ':' + s;
        break;
    }
    return result;
  }

  addZero(i: any) {
    if (i < 10) {
      i = '0' + i;
    }
    return i;
  }
}
